package com.pesilo.mvc.view.component.module.navigationModule
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.mvc.model.ModelLocator;
	import com.pesilo.net.http.HttpService;
	import com.pesilo.norms.Weather;
	import com.pesilo.ui.NavigationBar;
	import com.pesilo.util.UIUtil;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.ByteArray;
	import flash.utils.Timer;

	public class NavigationComponent extends BaseComponent
	{
		private var navigationCmp:NavigationBar;
		private var bg:Sprite;
		private var logo:Sprite;
		private var stars:MovieClip;
		
		private var menu:Sprite;
		private var homeBtn:Sprite;
		private var versionBtn:Sprite;
		private var soundBtn:Sprite;
		private var settingBtn:Sprite;
		private var weatherUI:MovieClip;
		
		private var timeTxt:TextField;
		private var timer:Timer;
		private var date:Date;
		
		private var user:Sprite;
		
		
//		private var lookBar:Boolean=false;//是否锁定导航条
		
		private var isSetting:Boolean=true;
		
		public function NavigationComponent()
		{
			super();
		}
		
		override public function initUI():void{
			navigationCmp=new NavigationBar();
			this.addChild(navigationCmp);
			bg=UIUtil.getElement(navigationCmp,"bg")as Sprite;
			logo=UIUtil.getElement(navigationCmp,"logo")as Sprite;
			stars=UIUtil.getElement(navigationCmp,"stars")as MovieClip;
			stars.visible=false;
			
			menu=UIUtil.getElement(navigationCmp,"menu")as Sprite;
			
			timeTxt=UIUtil.getElement(menu,"timeTxt")as TextField;
			timeTxt.autoSize=TextFieldAutoSize.CENTER;
			
			timer=new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,__timerEventHandle);
			timer.start();
			
			weatherUI=UIUtil.getElement(menu,"weatherUI")as MovieClip;
			
			homeBtn=UIUtil.getElement(menu,"homeBtn")as Sprite;
			homeBtn.buttonMode=true;
			homeBtn.visible=false;
			homeBtn.alpha=0;
			
			versionBtn=UIUtil.getElement(menu,"versionBtn")as Sprite;
			versionBtn.buttonMode=true;
//			versionBtn.visible=false;
//			versionBtn.alpha=0;
			soundBtn=UIUtil.getElement(menu,"soundBtn")as Sprite;
			soundBtn.buttonMode=true;
			
			settingBtn=UIUtil.getElement(menu,"settingBtn")as Sprite;
			settingBtn.buttonMode=true;
			settingBtn.addEventListener(MouseEvent.CLICK,__settingClickEventHandle);
			
			user=UIUtil.getElement(navigationCmp,"user")as Sprite;
			
			navigationCmp.y=-navigationCmp.height;
			TweenMax.to(navigationCmp,0.7,{delay:0.5,y:0,ease:Quad.easeIn(1,2,3,4)});
			if(!ModelLocator.lockBar){
				TweenMax.to(navigationCmp,0.7,{delay:10,y:-navigationCmp.height,ease:Quad.easeOut(1,2,3,4)});
			}
			
		}
		
		//设置按钮
		protected function __settingClickEventHandle(e:MouseEvent):void
		{
			
		}
		
		protected function __timerEventHandle(event:TimerEvent):void
		{
			date=new Date();
			timeTxt.text=date.hours+":"+date.minutes;
		}
		
		
		/**
		 * 设置天气
		 */
		public function setWeather(weather:String):void{
			if(weather==Weather.CLOUD){
				weatherUI.gotoAndStop(3);
			}else if(weather==Weather.SUN){
				weatherUI.gotoAndStop(2);
			}else if(weather==Weather.RAIN){
				weatherUI.gotoAndStop(4);
			}else if(weather==Weather.SNOW){
				weatherUI.gotoAndStop(5);
			}else{
				trace("unfind the "+weather+",use the cloudy!");
				weatherUI.gotoAndStop(1);
			}
		}
		
		/**
		 * 下拉导航条
		 */
		public function pullDown():void{
			TweenMax.killChildTweensOf(this);
			TweenMax.to(navigationCmp,0.7,{delay:0.5,y:0,ease:Quad.easeIn(1,2,3,4)});
			if(!ModelLocator.lockBar){
				TweenMax.to(navigationCmp,0.7,{delay:5,y:-bg.height,ease:Quad.easeOut(1,2,3,4)});
			}
		}
		
		override public function initEvent(b:Boolean=true):void{
			if(b){
				stage.addEventListener(MouseEvent.MOUSE_MOVE,__stageMouseMoveEventHandle);
				navigationCmp.addEventListener(MouseEvent.ROLL_OUT,__navigationRollOutEventHandle);
				
				logo.addEventListener(MouseEvent.ROLL_OVER,__mouseRollHandle);
				logo.addEventListener(MouseEvent.ROLL_OUT,__mouseRollHandle);
				
			}else{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,__stageMouseMoveEventHandle);
				navigationCmp.removeEventListener(MouseEvent.ROLL_OUT,__navigationRollOutEventHandle);
				
				logo.removeEventListener(MouseEvent.ROLL_OVER,__mouseRollHandle);
				logo.removeEventListener(MouseEvent.ROLL_OUT,__mouseRollHandle);
			}
		}
		
		protected function __mouseRollHandle(e:MouseEvent):void
		{
			switch(e.currentTarget){
				case logo:
					if(e.type==MouseEvent.ROLL_OVER){
						(logo as MovieClip).gotoAndStop(2);
					}else if(e.type==MouseEvent.ROLL_OUT){
						(logo as MovieClip).gotoAndStop(1);
					}
					break;
			}
		}
		
		protected function __navigationRollOutEventHandle(e:MouseEvent):void
		{
			if(!ModelLocator.lockBar){
				TweenMax.to(navigationCmp,0.7,{delay:1,y:-bg.height,ease:Quad.easeOut(1,2,3,4)});
			}
		}
		
		protected function __stageMouseMoveEventHandle(e:MouseEvent):void
		{
			if(mouseY<=navigationCmp.height){
				TweenMax.killChildTweensOf(this);
				TweenMax.to(navigationCmp,0.7,{y:0,ease:Quad.easeIn(1,2,3,4)});
			}
		}
		
		override public function initLocation():void{
			bg.width=stage.stageWidth;
			logo.x=50;

			menu.x=stage.stageWidth-menu.width-30;
			user.x=menu.x-user.width-12;
			
			stars.x=logo.x+logo.width/2;
		}
		
		
		
	}
}